%Run routine 
run functions\MicrostructureAllMkts;

%Run through microstructure routines and export price impact estimates,
%summary statistics, and standard errors to workspace for later usage

FileNames = {'output\ComprehensiveStudy_6AAll1','output\ComprehensiveStudy_6BAll1','output\ComprehensiveStudy_6CAll1','output\ComprehensiveStudy_6EAll1','output\ComprehensiveStudy_6JAll1','output\ComprehensiveStudy_6SAll1'};
valsForSumStats = zeros(2,6);
coeffs = zeros(315,6);
fullCoeffsDaily = cell(1,6);
fullCoeffs_FreeBetaDaily = cell(1,6);
fullCoeffsComposite = cell(1,6);
fullCoeffs_FreeBetaComposite = cell(1,6);
fullCoeffs_FreeBetaCompositeStError = cell(1,6);
alphas = zeros(2,6);
AdditionalValuesAll = cell(1,6);
GabaixKoijenStyleMultiplierExport = zeros(1,5);
counter = 0;
while(counter<5)
    counter = counter+1; 
    load(FileNames{1,counter},'X1All');
    valsForSumStats(1,counter) = sum(abs(X1All));
    valsForSumStats(2,counter) = size(X1All,1);
    GabaixKoijenStyleMultiplierExport(1,counter) = sum((abs(X1All).^0.5).*(abs(X1All)))/sum(abs(X1All));
    load(FileNames{1,counter},'betasExtrasMech05d');
    coeffs(:,counter) = betasExtrasMech05d(:,2);
    load(FileNames{1,counter},'betaMechComposite05');
    alphas(1,counter) = betaMechComposite05(2);
    load(FileNames{1,counter},'standardErrorsMechComposite05');
    alphas(2,counter) = standardErrorsMechComposite05(2);
    load(FileNames{1,counter},'AdditionalValues');
    AdditionalValuesAll{1,counter}=AdditionalValues;
    load(FileNames{1,counter},'betasExtrasMech05d');
    fullCoeffsDaily{1,counter}=betasExtrasMech05d;
    load(FileNames{1,counter},'betasExtrasMech');
    fullCoeffs_FreeBetaDaily{1,counter}=betasExtrasMech;
    load(FileNames{1,counter},'betaMech05d');
    fullCoeffsComposite{1,counter}=betaMech05d;
    load(FileNames{1,counter},'betaMechComposite');
    fullCoeffs_FreeBetaComposite{1,counter}=betaMechComposite;
    load(FileNames{1,counter},'standardErrorsMechComposite');
    fullCoeffs_FreeBetaCompositeStError{1,counter}=standardErrorsMechComposite;
end
coeffs(:,6) = coeffs(:,4)/2+coeffs(:,6);
alphas(1,6) = alphas(1,6) + alphas(1,4)/2; 
load(FileNames{1,5},'DatesForLater');
datesExtras = zeros(300,1);
counter = 0;
pointer = 1;
while(counter<6)
counter = counter +1;
lengthdates = size(DatesForLater{counter,1},1);
datesExtras(pointer:pointer+lengthdates-1,:) = DatesForLater{counter,1};
pointer = pointer + lengthdates;
end
datesExtras = datesExtras(1:pointer-1,:);

selectorNonHolidays = zeros(315,1)+1;
selectorNonHolidays([8 63 127 192 257 311:315],:) = 0;
selectorNonHolidays = (selectorNonHolidays>0);
formatOut = 'mm/dd/yy';
dateVec = datestr(datesExtras+1,formatOut);

coeffs = coeffs(selectorNonHolidays,:);
counter =0;
while(counter<5)
    counter = counter+1; 
    fullCoeffsDaily{1,counter} = fullCoeffsDaily{1,counter}(selectorNonHolidays,:);
    fullCoeffs_FreeBetaDaily{1,counter} = fullCoeffs_FreeBetaDaily{1,counter}(selectorNonHolidays,:);
    fullCoeffsComposite{1,counter} = fullCoeffsComposite{1,counter};
    fullCoeffs_FreeBetaComposite{1,counter} = fullCoeffs_FreeBetaComposite{1,counter};
end
oldFolder = cd('..');
save('intermediate\MicrostructureEstimatesAndOutput');
cd(oldFolder)



